package com.example.practicespring.controller.dataShowController;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.practicespring.domain.Server;
import com.example.practicespring.domain.ServerTask;
import com.example.practicespring.domain.User;
import com.example.practicespring.service.ServerService;
import com.example.practicespring.service.ServerTaskService;
import com.example.practicespring.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.example.practicespring.ConstantConfig.ConstantConfig.getTimeList;

@Slf4j
@RestController
@RequestMapping(value = "dataShow")
public class DataShowController {

    @Resource
    private ServerTaskService serverTaskService;

    @Resource
    private ServerService serverService;

    @Resource
    private UserService userService;

    private static final List<String> timeAll = getTimeList();

    @SaCheckRole(value = {"admin", "user", "tour"}, mode = SaMode.OR)
    @RequestMapping(value = "getTotalInfo", method = RequestMethod.GET)
    public SaResult getTotalInfo() {
        // 查询用户
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.eq("is_active", 1);
        long aCount = userService.count(userWrapper);
        long countAll = userService.count();
        // 查询服务器
        QueryWrapper<Server> serverQueryWrapper = new QueryWrapper<>();
        serverQueryWrapper.eq("state", 0);
        long sCount = serverService.count(serverQueryWrapper);
        long serverCountAll = serverService.count();
        // 查询任务
        long tCount = serverTaskService.count();
        // 任务时段
        long taskCountAll = sCount * 48;
        QueryWrapper<ServerTask> allTask = new QueryWrapper<>();
        List<ServerTask> taskInfo = serverTaskService.list(allTask);
        long useCount = 0;
        for (ServerTask t : taskInfo) {
            long timeCost = Arrays.stream(t.getTaskTime().split(",")).count();
            useCount += timeCost;
        }

        return SaResult.data(new ArrayList<>(Arrays.asList(serverCountAll, sCount, tCount, tCount, countAll, aCount, taskCountAll, useCount)));
    }

    @SaCheckRole(value = {"admin", "user", "tour"}, mode = SaMode.OR)
    @RequestMapping(value = "getServerInfo", method = RequestMethod.GET)
    public SaResult getServerInfo() {
        QueryWrapper<Server> serverQueryWrapper = new QueryWrapper<>();
        serverQueryWrapper.orderByAsc("name");
        List<Server> serverLists = serverService.list(serverQueryWrapper);

        // 增加一个禁用服务器的显示
        QueryWrapper<Server> serverDenyQueryWrapper = new QueryWrapper<>();
        serverDenyQueryWrapper.eq("state", 1);
        List<Server> serverDenyList = serverService.list(serverDenyQueryWrapper);
        List<String> denyList = serverDenyList.stream().map(Server::getName).collect(Collectors.toList());

        List<String> serverList = serverLists.stream().map(Server::getName).collect(Collectors.toList());
        List<Map<String, String>> result = new ArrayList<>();
        for (String s : serverList) {
            Map<String, String> map = new HashMap<>();
            map.put("name", s);

            // 对禁用服务器处理
            if(denyList.contains(s)){
                for (int i=1;i<49; i++){
                    map.put("t"+i, "2");
                }
                result.add(map);
                continue;
            }

            QueryWrapper<ServerTask> serverTaskQueryWrapper = new QueryWrapper<>();
            serverTaskQueryWrapper.eq("serverName", s);
            List<ServerTask> serverTaskList = serverTaskService.list(serverTaskQueryWrapper);

            Set<String> collect = Arrays.stream(
                    serverTaskList.stream().map(ServerTask::getTaskTime)
                            .collect(Collectors.joining(","))
                            .split(","))
                    .filter(str -> !StringUtils.isEmpty(str)).collect(Collectors.toSet());

            collect.forEach(time -> map.put("t" + (timeAll.indexOf(time) + 1), "1"));

            result.add(map);
        }
        log.info(result.toString());
        return SaResult.data(result);
    }

}
